﻿using System.Collections.Generic;
using S7PlcDataCollector.Models;
using S7PlcDataCollector.Service.Tools;

namespace S7PlcDataCollector.Service.SQLiteProxy
{
    // ReSharper disable once IdentifierTypo
    internal class SqliteDbProxy
    {
        public void Init()
        {
            // ReSharper disable once IdentifierTypo
            var sqliteHelper = new SqliteHelper();
            if (!sqliteHelper.InitDatabase())
            {
                LogHelper.AddErrorLog($"InitSQLite database failed: {sqliteHelper.LastErrorMsg}");
            }
        }

        #region PLC实体

        /// <summary>
        /// 获取PLC实体列表
        /// </summary>
        /// <returns>PLC实体列表</returns>
        public List<S7PlcEntity> GetS7PlcEntityList()
        {
            var s7PlcEntityList = new List<S7PlcEntity>();

            var bll = new Bll.S7PlcEntity();
            var models = bll.GetAllModels();
            s7PlcEntityList.AddRange(models);

            return s7PlcEntityList;
        }

        /// <summary>
        /// 添加PLC实体对象
        /// </summary>
        /// <param name="model">PLC实体对象</param>
        /// <returns>Rid</returns>
        public int AddS7PlcEntity(S7PlcEntity model)
        {
            var bll = new Bll.S7PlcEntity();
            return bll.Add(model);
        }

        /// <summary>
        /// 修改PLC实体对象
        /// </summary>
        /// <param name="model">PLC实体对象</param>
        /// <returns>true成功，false失败</returns>
        public bool UpdateS7PlcEntity(S7PlcEntity model)
        {
            var bll = new Bll.S7PlcEntity();
            return bll.Update(model);
        }

        /// <summary>
        /// 删除PLC实体对象
        /// </summary>
        /// <param name="model">PLC实体对象</param>
        /// <returns>true成功，false失败</returns>
        public bool DeleteS7PlcEntity(S7PlcEntity model)
        {
            var bll = new Bll.S7PlcEntity();
            return bll.Delete(model);
        }

        #endregion

        #region PLC变量实体

        /// <summary>
        /// 获取PLC变量实体列表
        /// </summary>
        /// <returns>PLC变量实体列表</returns>
        public List<S7PlcVariableEntity> GetS7PlcVariableEntityList()
        {
            var s7PlcEntityList = new List<S7PlcVariableEntity>();

            var bll = new Bll.S7PlcVariableEntity();
            var models = bll.GetAllModels();
            s7PlcEntityList.AddRange(models);

            return s7PlcEntityList;
        }

        /// <summary>
        /// 获取PLC变量实体列表
        /// </summary>
        /// <param name="s7PlcEntityRid">PLC实体Id</param>
        /// <returns>PLC变量实体列表</returns>
        public List<S7PlcVariableEntity> GetS7PlcVariableEntityList(int s7PlcEntityRid)
        {
            var s7PlcEntityList = new List<S7PlcVariableEntity>();

            var bll = new Bll.S7PlcVariableEntity();
            var models = bll.GetModels(s7PlcEntityRid);
            s7PlcEntityList.AddRange(models);

            return s7PlcEntityList;
        }

        /// <summary>
        /// 添加PLC变量实体对象
        /// </summary>
        /// <param name="model">PLC变量实体对象</param>
        /// <returns>Rid</returns>
        public int AddS7PlcVariableEntity(S7PlcVariableEntity model)
        {
            var bll = new Bll.S7PlcVariableEntity();
            return bll.Add(model);
        }

        /// <summary>
        /// 修改PLC变量实体对象
        /// </summary>
        /// <param name="model">PLC变量实体对象</param>
        /// <returns>true成功，false失败</returns>
        public bool UpdateS7PlcVariableEntity(S7PlcVariableEntity model)
        {
            var bll = new Bll.S7PlcVariableEntity();
            return bll.Update(model);
        }

        /// <summary>
        /// 删除PLC变量实体对象
        /// </summary>
        /// <param name="model">PLC变量实体对象</param>
        /// <returns>true成功，false失败</returns>
        public bool DeleteS7PlcVariableEntity(S7PlcVariableEntity model)
        {
            var bll = new Bll.S7PlcVariableEntity();
            return bll.Delete(model);
        }

        #endregion
    }
}
